// src/com/q18/jenkins/Email.groovy

package com.q18.jenkins
import com.q18.jenkins.EnvUtils

public class Email {
    public static void  sendEmail(def script, String status, String to) {
        def emoji = status == 'SUCCESS' ? '✅' : '❌'
        def statusName = status == 'SUCCESS' ? '成功' : '失败'

        String jobName = EnvUtils.getEnv(script, "JOB_NAME", "")
        String buildNumber = EnvUtils.getEnv(script, "BUILD_NUMBER", "")
        String buildUrl = EnvUtils.getEnv(script, "BUILD_URL", "")

        script.echo "[ShareLib] sendEmail JobName: ${jobName}, BuildNumber: ${buildNumber}, BuildURL: ${buildUrl}"

        script.emailext (
            to: to,
            subject: "${emoji}构建${statusName}: ${jobName} - Build #${buildNumber}",
            body: """\
            尊敬的系统管理员：

            ${emoji}「${jobName}」项目，构建${statusName}！

            ▌构建信息
            项目名称：${jobName}
            构建编号：${buildNumber}
            构建状态：${statusName}
            构建日志：${buildUrl}

            ▌操作建议
            请查看详情，并进行后续操作。

            此致,
            Jenkins
            """.stripIndent()
        )
    }
}